﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Projeto3D
{
    class Personagem3D : Object3D
    {

       KeyboardState teclado;
        MouseState mouse;
        bool controleAnimacao;

        Tiro.Tipo tipoArma = Tiro.Tipo.Normal;

        Vector3 direcao = Vector3.Zero;

        float speed = 0.3f;

        float delay = 0;
        float delaymax = 10;

        public Personagem3D(Model model)
            : base(model)
        {
        }

        private Vector3 MousePosition(MouseState ms, Camera3D camera)
        {
            Vector3 nearScreenPoint = new Vector3(ms.X, ms.Y, 0);
            Vector3 farScreenPoint = new Vector3(ms.X, ms.Y, 1);
            Vector3 nearWorldPoint = Game1.self.GraphicsDevice.Viewport.Unproject(nearScreenPoint, camera.projection, camera.View, Matrix.Identity);
            Vector3 farWorldPoint = Game1.self.GraphicsDevice.Viewport.Unproject(farScreenPoint, camera.projection, camera.View, Matrix.Identity);

            Vector3 direction = farWorldPoint - nearWorldPoint;

            float zFactor = -nearWorldPoint.Y / direction.Y;
            Vector3 zeroWorldPoint = nearWorldPoint + direction * zFactor;

            return zeroWorldPoint;
        }

        private void VerificaRotacao(Camera3D camera)
        {
            Vector3 temp = MousePosition(mouse, camera);
            this.direcao = temp - this.position;
        }

        public Vector2 update(GameTime gameTime, Camera3D camera)
        {
            Vector2 speed = new Vector2();

            teclado = Keyboard.GetState();
            mouse = Mouse.GetState();

            //criar um codigo pra mante a nave na tela
            if (teclado.IsKeyDown(Keys.W) && Game1.boxGame.Y * (-1) - 1 < this.position.Z)
            {
                this.position.Z -= 0.5f;
                speed.Y = 5f; 
            }

            if (teclado.IsKeyDown(Keys.S) && Game1.boxGame.Y > this.position.Z)
            {
                this.position.Z += 0.5f;
                speed.Y = -5f;
            }

            if (teclado.IsKeyDown(Keys.A) && Game1.boxGame.X * (-1) < this.position.X)
            {
                this.position.X -= 0.5f;
                speed.X = 5f;
            }

            if (teclado.IsKeyDown(Keys.D) && Game1.boxGame.X - 7 > this.position.X)
            {
                this.position.X += 0.5f;
                speed.X = -5f;
            }

            VerificaRotacao(camera);

            if (delay > 0)
            {
                --delay;
            }

            return speed;
        }

    }
}
